package test06;

/**
 * 连续批处理策略
 */
public class ContinuousBatchingStrategy implements InferenceOptimizationStrategy {
    private int maxBatchSize = 16;
    private boolean preemptionEnabled = true;

    @Override
    public Tensor[] optimizeInference(Model model, RequestData[] requests) {
        System.out.println("⚡ 应用连续批处理策略");
        System.out.println("   - 最大批处理大小: " + maxBatchSize);
        System.out.println("   - 抢占机制: " + (preemptionEnabled ? "启用" : "禁用"));

        // 模拟连续批处理
        System.out.println("   - 动态添加新请求到运行中的批次");
        System.out.println("   - 优化GPU利用率同时减少等待时间");

        Tensor[] preprocessedInputs = preprocessInputs(requests);
        return model.forward(preprocessedInputs);
    }

    private Tensor[] preprocessInputs(RequestData[] requests) {
        Tensor[] inputs = new Tensor[requests.length];
        for (int i = 0; i < requests.length; i++) {
            inputs[i] = new Tensor(new float[]{1.0f, 0.0f, 0.5f}, new int[]{1, 3});
        }
        return inputs;
    }

    public void setMaxBatchSize(int maxBatchSize) {
        this.maxBatchSize = maxBatchSize;
    }

    public void enablePreemption(boolean enabled) {
        this.preemptionEnabled = enabled;
    }
}
